from django.shortcuts import render

# Create your views here.
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import BaseFilterBackend
from rest_framework.viewsets import ModelViewSet

from vue.models import Record
from vue.serializers import RecordSerializer

class Agefilter(BaseFilterBackend):
    def filter_queryset(self, request, queryset, view):
        tfs = request.query_params.get('age')
        if tfs and ',' in tfs:
            start, stop = str(tfs).split(',')
            if start and stop:
                queryset = queryset.filter(age__gte=int(start),age__lte=int(stop))
            elif start:
                queryset = queryset.filter(age__gte=int(start))
            elif stop:
                queryset = queryset.filter(age__lte=int(stop))
        return queryset

class RecordDRFView(ModelViewSet):
    queryset = Record.objects.all()
    serializer_class = RecordSerializer
    filterset_fields = ('city', 'degree')  # 指定过滤的字段
    filter_backends = [Agefilter, DjangoFilterBackend]